import {
	baseURL,
	baseImgUrl,
	Appid
} from '../config/config.js'
export function share(scene, provider, summary, type) {
	return new Promise(function(resolve, reject) {
		uni.share({
			title: '有标题吗?',
			provider: provider || "weixin",
			scene: scene || "WXSceneSession",
			type: type || 0,
			summary: summary || '分享',
			success: function(res) {
				resolve(res)
				console.log("success:" + JSON.stringify(res));
			},
			fail: function(err) {
				console.log("fail:" + JSON.stringify(err));
				reject(err)
			}
		});
	})
}
export function addImgUrl(arr, key) {
	if (isObjArr(arr) == 'Array') {
		arr.forEach(item => {
			if (item[key] && !item[key].includes("http://111.229.30.99:8888/")) {
				item[key] = baseImgUrl + item[key]
			}
		})
		return arr
	} else if (isObjArr(arr) == 'object') {
		if (arr[key] && !arr[key].includes("http://111.229.30.99:8888/")) {
			arr[key] = baseImgUrl + arr[key]
		}
		return arr
	} else {
		return arr
		// uni.showToast({
		// 	title: '请传入正确的数据类型'
		// })
	}

}
export function isObjArr(value) {
	if (Object.prototype.toString.call(value) === "[object Array]") {
		return 'Array'
	} else if (Object.prototype.toString.call(value) === '[object Object]') {
		return 'object'
	} else {
		return false
	}
}
export function domScroll(idArr, index, navHeight, _this, info) {
	uni.createSelectorQuery().select(idArr[index]).boundingClientRect(function(ret) {
		var query = uni.createSelectorQuery().in(_this);
		query.selectViewport().scrollOffset()
		query.select(idArr[index]).boundingClientRect();
		query.exec(function(res) {
			if (info) {
				info.boundingClientRect(function(data) { //data - 各种参数
					console.log(data)
					var miss = res[0].scrollTop + res[1].top - 10;
					uni.pageScrollTo({
						scrollTop: miss - navHeight - data.height,
						duration: 300
					});
				}).exec()
			} else {
				var miss = res[0].scrollTop + res[1].top - 10;
				console.log(miss, _this.navHeight)
				uni.pageScrollTo({
					scrollTop: miss - _this.navHeight,
					duration: 300
				});
			}

		});
	}).exec()
}
export function subPay(res) {
	return new Promise(function(resolve, reject) {
		uni.requestPayment({
			appid: res.appId,
			package: res.packageStr,
			nonceStr: res.nonceStr,
			timeStamp: res.timeStamp,
			signType: 'MD5',
			provider: 'wxpay',
			paySign: res.paySign,
			success: function(res) {
				console.log('success:' + JSON.stringify(res));
				resolve(res)
			},
			fail: function(err) {
				reject(err)
				console.log('fail:' + JSON.stringify(err));
			}
		});
	})
}
